package org.flexlancer.panel
{
	import flash.events.MouseEvent;
	
	import mx.containers.Panel;
	import mx.containers.VBox;
	
	import org.flexlancer.Logger.Logger;
	import org.flexlancer.controller.Picture;
	import org.flexlancer.events.PictureDoubleClickEvent;

	public class Gallary extends Panel
	{
		private var collectionImage:Array = new Array();
		private var container:VBox = new VBox();
		public var Selected:Picture = null;
		///// shahid code
	//	public var caller:Gallary = new Gallary;
		
		///shahihd code ends
		public function Gallary()
		{
			super();
			this.addChild(this.container);
		}
		
		public function get Collection():Array{
			return this.collectionImage;
		}
		public function AddPicture(parm:Picture):void{
			try{
				this.collectionImage.push(parm);
				this.container.addChild(parm);
				parm.addEventListener(MouseEvent.MOUSE_DOWN,this.ItemClick);
				/////shahid code
				parm.doubleClickEnabled = true;
				parm.addEventListener(MouseEvent.DOUBLE_CLICK,this.ItemDoubleClick);
				////shahid code ends
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		public function RemoveImage(parmImageName:String):Picture{
			try{
				var removedItem:Picture = null;
				var index:Number = 0;
				
				for each ( var singleImage:Picture in this.collectionImage ) {
					if ( singleImage.Name == parmImageName ){
						removedItem = singleImage;
						this.collectionImage.splice(index,1);
						this.container.removeChild(removedItem);
						break;
					}
					index++;
				}
				
				return removedItem;
			}catch(err:Error){
				Logger.LogE(err);
			}
			return null;
		}
		public function RemoveSelected():Picture{
			try{
				if ( this.Selected == null ) return null;
				this.RemoveImage(this.Selected.Name);
			}catch(err:Error){
				Logger.LogE(err);
			}finally{
				this.Selected = null;
			}
			return this.Selected;
		}
		public function ItemClick(evt:MouseEvent):void{
			try{
				if ( this.Selected != null ){
					this.Selected.Selected = false;
				}
				this.Selected = Picture(evt.currentTarget);
				
				this.Selected.Selected = true;
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		
		////shahid code------
		public function ItemDoubleClick(evt:MouseEvent):void{
			try{
				if ( this.Selected != null ){
					this.Selected.Selected = false;
				}
				this.Selected = Picture(evt.currentTarget);
				
				this.Selected.Selected = true;
				
				var send_evt:PictureDoubleClickEvent = new PictureDoubleClickEvent(PictureDoubleClickEvent.PICTURE_DOUBLE_CLICK);
				send_evt.selected_picture = this.Selected;
				
				this.dispatchEvent(send_evt);
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
		////////shahid code ends
	}
}